package com.xuxin.baby.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.xuxin.baby.R;
import com.xuxin.baby.adapter.UserTicketAdapter;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.base.BaseFragment;
import com.xuxin.baby.bean.UserTicketBean;
import com.xuxin.baby.contract.UserTicketContract;
import com.xuxin.baby.presenter.UserTicketPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.RecycleUtils;

import java.util.Iterator;

import butterknife.BindView;

/**
 * Created by xu_xin
 * Created on 2020/12/4 0004
 */
public class TicketFragment extends BaseFragment implements UserTicketContract.View {
    private final UserTicketContract.Presenter presenter = new UserTicketPresenter(this);
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smart_refresh;
    @BindView(R.id.smart_recycle)
    RecyclerView smart_recycle;

    private UserTicketAdapter myAdapter;
    private String memberId;//用户ID
    private String tenantId;//租户id
    private String token;//授权token
    private int type;

    public static TicketFragment newInstance(int type) {
        TicketFragment fragment = new TicketFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View setContentView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.fragment_smart, container, false);
    }

    @Override
    public void initView() {
        smart_refresh.setEnableLoadMore(false);
        smart_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LogUtils.e("onRefresh");
                myAdapter.setList(null);
                handler.sendEmptyMessage(0);
            }
        });
        myAdapter = new UserTicketAdapter();
        RecycleUtils.init(smart_recycle);
        smart_recycle.setAdapter(myAdapter);
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(context);
        memberId = aCache.getAsString(BaseConstant.memberId);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        if (null != getArguments()) {
            type = getArguments().getInt("type");
        }
        smart_refresh.autoRefresh();
    }

    @Override
    public void onComplete() {
        smart_refresh.finishRefresh();
        smart_refresh.finishLoadMore();
    }

    @Override
    public void onClick(View view) {

    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.getUserTicket(tenantId, token, memberId);
            }
            return false;
        }
    });

    @Override
    public void onSuccess(UserTicketBean userTicketBean) {
        if (type == 0) {
            myAdapter.setList(userTicketBean.getResult().getItems());
        } else if (type == 1) {
            try {
                Iterator<UserTicketBean.ResultBean.ItemsBean> iterator = userTicketBean.getResult().getItems().iterator();
                while (iterator.hasNext()) {
                    UserTicketBean.ResultBean.ItemsBean itemsBean = iterator.next();
                    if (itemsBean.getItemType() != 0) {
                        iterator.remove();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                myAdapter.setList(userTicketBean.getResult().getItems());
            }
        } else if (type == 2) {
            try {
                Iterator<UserTicketBean.ResultBean.ItemsBean> iterator = userTicketBean.getResult().getItems().iterator();
                while (iterator.hasNext()) {
                    UserTicketBean.ResultBean.ItemsBean itemsBean = iterator.next();
                    if (itemsBean.getItemType() != 1) {
                        iterator.remove();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                myAdapter.setList(userTicketBean.getResult().getItems());
            }
        }
    }
}